home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / emcs1857 / 1857el~1.zoo / lisp / man.el < prev    next >
Encoding:
Text File  |  1992-02-04  |  5.4 KB  |  167 lines

  1. ;; Read in and display parts of Unix manual.
  2. ;; Copyright (C) 1985, 1986, 1990 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. ;; Modified 1990 for 8-bit character support by Howard Gayle.
  22. ;; See case-table.el for details.
  23.  
  24. (defun default-manual-entry-hook ()
  25.    "Nuke underlining and overstriking (only by the same letter)."
  26.    (goto-char (point-min))
  27.    (while (search-forward "\b" nil t)
  28.       (let* (
  29.               (preceding (char-after (- (point) 2)))
  30.         (following (following-char))
  31.         )
  32.      (cond
  33.         ((= preceding following) ; x\bx
  34.            (delete-char -2))
  35.         ((= preceding ?\_) ;; _\b
  36.            (delete-char -2))
  37.         ((= following ?\_) ;; \b_
  38.            (delete-region (1- (point)) (1+ (point))))
  39.      )
  40.       )
  41.    )
  42. )
  43.  
  44. (defvar manual-entry-hook 'default-manual-entry-hook
  45.    "Hook run before displaying a manual entry.")
  46.  
  47. (defun manual-entry (topic &optional section)
  48.   "Display the Unix manual entry for TOPIC.
  49. TOPIC is either the title of the entry, or has the form TITLE(SECTION)
  50. where SECTION is the desired section of the manual, as in `tty(4)'."
  51.   (interactive "sManual entry (topic): ")
  52.   (if (and (null section)
  53.        (string-match "\\`[ \t]*\\([^( \t]+\\)[ \t]*(\\(.+\\))[ \t]*\\'" topic))
  54.       (setq section (substring topic (match-beginning 2)
  55.                      (match-end 2))
  56.         topic (substring topic (match-beginning 1)
  57.                    (match-end 1))))
  58.   (with-output-to-temp-buffer "*Manual Entry*"
  59.     (buffer-flush-undo standard-output)
  60.     (save-excursion
  61.       (set-buffer standard-output)
  62.       (message "Looking for formatted entry for %s%s..."
  63.            topic (if section (concat "(" section ")") ""))
  64.       (let ((dirlist manual-formatted-dirlist)
  65.         (case-fold-search nil)
  66.         name)
  67.     (if (and section (or (file-exists-p
  68.                    (setq name (concat manual-formatted-dir-prefix
  69.                           (substring section 0 1)
  70.                           "/"
  71.                           topic "." section)))
  72.                  (file-exists-p
  73.                    (setq name (concat manual-formatted-dir-prefix
  74.                           section
  75.                           "/"
  76.                           topic "." section)))))
  77.         (insert-man-file name)
  78.       (while dirlist
  79.         (let* ((dir (car dirlist))
  80.            (name1 (concat dir "/" topic "."
  81.                   (or section
  82.                       (substring
  83.                     dir
  84.                     (1+ (or (string-match "\\.[^./]*$" dir)
  85.                         -2))))))
  86.            completions)
  87.           (if (file-exists-p name1)
  88.           (insert-man-file name1)
  89.         (condition-case ()
  90.             (progn
  91.               (setq completions (file-name-all-completions
  92.                      (concat topic "." (or section ""))
  93.                      dir))
  94.               (while completions
  95.             (insert-man-file (concat dir "/" (car completions)))
  96.             (setq completions (cdr completions))))
  97.           (file-error nil)))
  98.           (goto-char (point-max)))
  99.         (setq dirlist (cdr dirlist)))))
  100.  
  101.       (if (= (buffer-size) 0)
  102.       (progn
  103.         (message "No formatted entry, invoking man %s%s..."
  104.              (if section (concat section " ") "") topic)
  105.         (if section
  106.         (call-process manual-program nil t nil section topic)
  107.             (call-process manual-program nil t nil topic))
  108.         (if (< (buffer-size) 80)
  109.         (progn
  110.           (goto-char (point-min))
  111.           (end-of-line)
  112.           (error (buffer-substring 1 (point)))))))
  113.  
  114.       (message "Cleaning manual entry for %s..." topic)
  115.       (nuke-nroff-bs)
  116.       (set-buffer-modified-p nil)
  117.       (message ""))))
  118.  
  119. ;; Hint: BS stands for more things than "back space"
  120. (defun nuke-nroff-bs ()
  121.   (interactive "*")
  122.   ;; Nuke headers: "MORE(1) UNIX Programmer's Manual MORE(1)"
  123.   (goto-char (point-min))
  124.   (while (re-search-forward "^ *\\([A-Za-z][-_A-Za-z0-9]*([0-9A-Z]+)\\).*\\1$" nil t)
  125.     (replace-match ""))
  126.   
  127.   ;; Nuke footers: "Printed 12/3/85    27 April 1981    1"
  128.   ;;    Sun appear to be on drugz:
  129.   ;;     "Sun Release 3.0ī€žBī€ž  Last change: 1 February 1985     1"
  130.   ;;    HP are even worse!
  131.   ;;     "     Hewlett-Packard   -1- (printed 12/31/99)"  FMHWA12ID!!
  132.   ;;    System V (well WICATs anyway):
  133.   ;;     "Page 1              (printed 7/24/85)"
  134.   ;;    Who is administering PCP to these corporate bozos?
  135.   (goto-char (point-min))
  136.   (while (re-search-forward
  137.        (cond ((eq system-type 'hpux)
  138.           "^[ \t]*Hewlett-Packard\\(\\| Company\\)[ \t]*- [0-9]* -.*$")
  139.          ((eq system-type 'usg-unix-v)
  140.           "^ *Page [0-9]*.*(printed [0-9/]*)$")
  141.          (t
  142.           "^\\(Printed\\|Sun Release\\) [0-9].*[0-9]$"))
  143.        nil t)
  144.     (replace-match ""))
  145.  
  146.   ;; Crunch blank lines
  147.   (goto-char (point-min))
  148.   (while (re-search-forward "\n\n\n\n*" nil t)
  149.     (replace-match "\n\n"))
  150.  
  151.   ;; Nuke blanks lines at start.
  152.   (goto-char (point-min))
  153.   (skip-chars-forward "\n")
  154.   (delete-region (point-min) (point))
  155.   (run-hooks 'manual-entry-hook))
  156.  
  157.  
  158.  
  159. (defun insert-man-file (name)
  160.   ;; Insert manual file (unpacked as necessary) into buffer
  161.   (if (or (equal (substring name -2) ".Z")
  162.       (string-match "/cat[0-9][a-z]?\\.Z/" name))
  163.       (call-process "zcat" name t nil)
  164.     (if (equal (substring name -2) ".z")
  165.     (call-process "pcat" nil t nil name)
  166.       (insert-file-contents name))))
  167.